// pages/mailList/mailList.js
//获取应用实例
const app = getApp();
const api = require('../../utils/api');
const constant = require('../../utils/constant');
const storage = require('../../utils/storage');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //获取全局变量 导航栏的高度statusBarHeight
    statusBarHeight: app.globalData.statusBarHeight,
    // 胶囊
    jiaonang: app.globalData.jiaonang, 
    navigationHeight: app.globalData.navigationHeight,
    // 底部菜单高度
    tabbarHeight: app.globalData.tabbarHeight,
    BASEURLWWW:constant.BASEURLWWW,
    BASEURL: constant.BASEURL,
    // 选项卡
    tab:[
      {
        type: 1,
        title: '借货人'
      },
      {
        type: 2,
        title: '供货商'
      }
    ],
    tabType: 2,
    getOwnerListOption: {
      store_id: 0,
      keyword: '',
      owner_phonetic: ''
    },
    // 滑动菜单
    scrollSide:["Star","A","B","C","D","E","F","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","~"],
    scrollCurrent: "",
    // 星标朋友
    mailList:[
      {
        url: "https://img2.baidu.com/it/u=361828693,3037234327&fm=26&fmt=auto&gp=0.jpg",
        name: '景甜景甜景甜景甜景甜景甜'
      },
      {
        url: "https://img2.baidu.com/it/u=361828693,3037234327&fm=26&fmt=auto&gp=0.jpg",
        name: '景甜'
      }
    ]
  },
  onLoad(option){
    this.getOwnerListData()
  },
  // 获取货主列表
  getOwnerListData(){
    api.getOwnerList(this.data.getOwnerListOption).then(res => {
      console.info(res)
      if(res.code == 0){
        this.setData({
          mailList: [...res.data.data]
        })
      }
    })
  },
  // 选项卡
  tabChange(e){
    let tabType = e.currentTarget.dataset.type
    if(this.data.tabType != tabType){
      this.setData({
        tabType
      })
      if(tabType == 1){ // 借货人

      }else{ // 供货商
        this.getOwnerListData()
      }
    }
  },
  // 跳转到添加供货商
  jumpToAddOwner(){
    wx.reLaunch({
      url: '/pages/addOwner/addOwner',
    })
  },
  //手指触摸动作开始 记录起点X坐标
  touchstart: function (e) {
    //开始触摸时 重置所有删除
    this.data.mailList.forEach(item => {
      item.owner.forEach(function (v, i) {
        if (v.isTouchMove)//只操作为true的
          v.isTouchMove = false;
      })
    })
    this.setData({
      startX: e.changedTouches[0].clientX,
      startY: e.changedTouches[0].clientY,
      mailList: this.data.mailList
    })
  },
  //滑动事件处理
  touchmove: function (e) {
    var that = this,
      index = e.currentTarget.dataset.index,//当前索引
      startX = that.data.startX,//开始X坐标
      startY = that.data.startY,//开始Y坐标
      touchMoveX = e.changedTouches[0].clientX,//滑动变化坐标
      touchMoveY = e.changedTouches[0].clientY,//滑动变化坐标
      //获取滑动角度
      angle = that.angle({ X: startX, Y: startY }, { X: touchMoveX, Y: touchMoveY });
      that.data.mailList.forEach(item => {
        item.owner.forEach(function (v, i) {
          v.isTouchMove = false
          //滑动超过30度角 return
          if (Math.abs(angle) > 30) return;
          if (v.owner_id == index) {
            if (touchMoveX > startX) //右滑
              v.isTouchMove = false
            else //左滑
              v.isTouchMove = true
          }
        })
      })
    //更新数据
    that.setData({
      mailList: that.data.mailList
    })
  },
  /**
   * 计算滑动角度
   * @param {Object} start 起点坐标
   * @param {Object} end 终点坐标
   */
  angle: function (start, end) {
    var _X = end.X - start.X,
      _Y = end.Y - start.Y
    //返回角度 /Math.atan()返回数字的反正切值
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },
  // 删除
  deleteOwner(e){
    let item = e.currentTarget.dataset.item
    let that = this
    console.info(item)
    wx.showModal({
      title: '提示',
      content: `确定删除货主[${item.owner_name}]?`,
      success (res) {
        if (res.confirm) {
          api.deleteOwner({owner_id:item.owner_id.toString()}).then(res => {
            console.info(res)
            if(res.code == 0){
              wx.showToast({
                title: '成功!',
                icon: 'none'
              })
              that.getOwnerListData()
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
  },
  // 滚动到指定位置
  scrollTo(e){
    let item = e.currentTarget.dataset.item
    this.setData({
      scrollCurrent: `scrollTo${item}`
    })
  },
  // 滚动到特定位置
  scrollToOther(e){
    this.setData({
      scrollCurrent: e.currentTarget.dataset.item
    })
  },
  // 跳转到便签
  jumpToNote(){
    wx.navigateTo({
      url: '/pages/note/note',
    })
  },
  // 返回到首页
  back:function(){
    wx.reLaunch({
      url: '/pages/personal/personal'
    })
  },
  // 背景不滚动
  dontMove: function(){
    return;
  },
  
})